import {Injectable,OnInit} from '@angular/core';

import {Http, Response,Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Rx';
import  'rxjs/add/operator/map';

import {User,Message} from './user';
import {Picture}from'../pictrue/picture';
import {SignComponent} from './sign.component';
import {PictrueComponent} from '../pictrue/pictrue.component';
//mobile是参数 ；user.mobile传入的具体参数值
const CONTACT_URL='http://192.168.16.188:8000/messagebus/';//+user.mobile;
const CONTACT_APPID="wx598836973839e0b9";
const CONTACT_URL2='../assets/data/mobile.json';
const CONTACT_URL3='../assets/data/picture.json';
const CONTACT_URL4='../assets/data/message.json';
const CONTACT_URL5='v1/uzer/wx598836973839e0b9/bindingWechat';

const CONTACT_URL6='../assets/data/weixin.bind.json';

@Injectable()

export class UserService {
 loading:boolean;
 data:any;

 constructor(private http: Http) {}
  //通过get获取用户信息
  verifyUser(user:User): Observable<any>{

      let url =CONTACT_URL+"v1/uzer/"+CONTACT_APPID+"/isExistUzerByMobile?mobile="+user.mobile;
      //let url = CONTACT_URL2 +"?mobile="+user.mobile;
      let ores = this.http.get(url).map(this.extractData);
      return  ores;


  }

  private extractData(res:Response){
   let body = res.json();
   this.loading = false;
    return body.data||{};
  }

// 调用方法

  //通过get验证图片
  checkPicture(captcha:any,token:string):Observable<any>{

      let url3 = CONTACT_URL +"v1/uzer/systemValidateCode"+"?code="+captcha+"&&"+"uuid="+token;
      //let url3 = CONTACT_URL3+"?code="+captcha+"&&"+"uuid="+token;
      let ores = this.http.get(url3).map(this.extractData);
      return  ores;
  }


  //通过get发送请求 获取验证短息信息接口
  getMessage(data:any):Observable<any>{
    console.log("user.mobile是:"+data);
    let url4 = CONTACT_URL+"v1/uzer/sendValidateCode"+"?mobile="+data;
   //let url4 = CONTACT_URL4+"?mobile="+data;

    let ores = this.http.get(url4).map(this.extractData);
    return ores

  }
  //通过get发送请求  获取手机验证验证码接口
  checkMessage(user:User):Observable<any>{

    let url4 = CONTACT_URL+"v1/uzer/validateCode"+"?mobile="+user.mobile+"&&"+"code="+user.code;
  //  let url4 = CONTACT_URL4+"?mobile="+user.mobile+"&&"+"code="+user.code;
  //  let url = " https://angular-2-training-book.rangle.io/handout/routing/routeparams.html ";
    let ores = this.http.get(url4).map(this.extractData);
    return ores;
  }

   //微信绑定 接口
   weixinBind(user:User,openId:string):Observable<any>{

    let url6 =CONTACT_URL+CONTACT_URL5;
    let postParam= {
               "openId": openId,
               "mobile": user.mobile
            };

     let   body = JSON.stringify(postParam);
     console.log(body);
     let headers = new Headers({'Content-Type':'application/json'});
     let options = new RequestOptions({headers:headers});
     let ores = this.http.post(url6,body,options).map(this.extractData);
     //let ores = this.http.post("http://jsonplaceholder.typicode.com/posts",body,options).map(this.extractData);


     return ores;

   }

}
